package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/10 16:01
 * https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
 */
public class Title1047 {

    public static String removeDuplicates(String s) {
        if (s.length() == 1) {
            return s;
        }
        char[] result = s.toCharArray();
        int left = 0;
        int right = 0;
        while (right < result.length) {
            result[left] = result[right];
            if (left == 0 || result[left] != result[left - 1]) {
                left++;
            } else {
                left--;
            }
            right++;
        }
        return new String(result, 0, left);
    }





    public static String removeDuplicates1(String s) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (result.length() == 0 || c != result.charAt(result.length() - 1)) {
                result.append(c);
            }else{
                result.deleteCharAt(result.length() - 1);
            }
        }
        return result.toString();
    }


    public static String removeDuplicates2(String s) {
        char[] chars = s.toCharArray();
        int slow = 0;
        for (int fast = 0; fast < chars.length; fast++) {
            if (slow == 0 || chars[slow - 1] != chars[fast]) {
                chars[slow++] = chars[fast];
            } else {
                slow--;
            }
        }
        return new String(chars, 0, slow);
    }











    public static void main(String[] args) {
        removeDuplicates1("abbaca");
    }
}
